package com.qymerge.manage.base.controller;

import com.google.common.reflect.TypeToken;
import com.qymerge.manage.base.dto.MetaProfitReportItemDTO;
import com.qymerge.manage.base.ro.MetaBudgetRO;
import com.qymerge.manage.base.service.IMetaBudgetService;
import com.qymerge.manage.core.bean.*;
import com.qymerge.manage.core.constant.ParamKey;
import com.qymerge.manage.core.controller.BaseController;
import com.qymerge.manage.core.exception.ApiException;
import com.qymerge.manage.core.utils.*;
import com.qymerge.manage.system.ro.SysUserRO;
import com.qymerge.manage.system.service.ISystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>Title: IMetaBudgetManageController</p>
 * <p>Description: 预算表</p>
 *
 * @author Xiao
 * @date 2025/4/29 17:18
 * @version 1.0.0
 */
@Controller
@RequestMapping(value = "base/budget")
public class IMetaBudgetManageController extends BaseController {

    @Autowired
    private IMetaBudgetService metaBudgetService;
    @Autowired
    private ISystemService iSystemService;
    
    /**
     * 列表
     *
     * @param parameters
     * @param callback
     */
    @RequestMapping("list")
    public void list(HttpServletRequest request, HttpServletResponse response,

                     @RequestParam(defaultValue = ParamKey.In.dlt_parameters) String parameters,
                     @RequestParam(defaultValue = ParamKey.In.empty) String callback) throws Exception {
        ParamValue<MetaBudgetRO> paramValue = GsonUtil.json2T(parameters, new TypeToken<ParamValue<MetaBudgetRO>>() {
        }.getType());
        ParamCommon common = paramValue.getCommon();
        MetaBudgetRO ro = paramValue.getData();

        // 非系统管理员，登录用户根据绑定的关联公司查询数据
        SysUserRO sysUser = iSystemService.getSysUser(common.getUid());
        if (ObjectUtils.isEmpty(sysUser)) {
            RequestCheckUtils.checkNotFalse( false,  "请确认登录后，在请求数据");
        }
        if(common.getUid() != -1) {
            ro.setCompanyId(sysUser.getCompany_id());
        }

        DataTableRO data = metaBudgetService.list(ro, new PageRequest(common.getPage(), common.getSize()));
        PrintCodeUtils.printSuccess(response, data.build(), callback);
    }

    /**
     * 详情
     *
     * @param request
     * @param response
     * @param callback
     * @throws Exception
     */
    @RequestMapping("detail")
    public void detail(HttpServletRequest request, HttpServletResponse response,

                       @RequestParam(defaultValue = ParamKey.In.dlt_parameters) String parameters,
                       @RequestParam(defaultValue = ParamKey.In.empty) String callback) throws Exception {

        ParamValue<MetaBudgetRO> paramValue = GsonUtil.json2T(parameters, new TypeToken<ParamValue<MetaBudgetRO>>() {
        }.getType());
        ParamCommon common = paramValue.getCommon();
        MetaBudgetRO ro = paramValue.getData();

        RequestCheckUtils.checkNotEmpty(ro.getId(), "ID必须");
        MetaBudgetRO resultRO = metaBudgetService.detail(ro.getId());

        PrintCodeUtils.printSuccess(response, resultRO, callback);
    }

    /**
     * 增加
     *
     * @param request
     * @param response
     * @param callback
     */
    @RequestMapping("save")
    public void save(HttpServletRequest request, HttpServletResponse response,

                     @RequestParam(defaultValue = ParamKey.In.dlt_parameters) String parameters,
                     @RequestParam(defaultValue = ParamKey.In.empty) String callback) throws Exception {
        ParamValue<MetaBudgetRO> paramValue = GsonUtil.json2T(parameters, new TypeToken<ParamValue<MetaBudgetRO>>() {
        }.getType());
        ParamCommon common = paramValue.getCommon();
        MetaBudgetRO ro = paramValue.getData();

        // 非系统管理员，登录用户根据绑定的关联公司查询数据
        SysUserRO sysUser = iSystemService.getSysUser(common.getUid());
        if (ObjectUtils.isEmpty(sysUser)) {
            RequestCheckUtils.checkNotFalse( false,  "请确认登录后，在操作数据");
        }
        ro.setCreate_user_id(common.getUid());
        ro.setCreate_user_name(sysUser.getCn_name());
        ro.setCreate_time(System.currentTimeMillis());

        if (ro.getOrgId() <= 0) {
            RequestCheckUtils.checkNotEmpty(ro.getId(), "管理口径组织架构必须");
        }

        ActionResultRO resultRO = metaBudgetService.save(ro);
        PrintCodeUtils.print(response, resultRO, callback);
    }

    /**
     * 删除
     *
     * @param parameters
     * @return
     */
    @RequestMapping(value = "/delete")
    public void delete(HttpServletRequest request, HttpServletResponse response,

                       @RequestParam(defaultValue = ParamKey.In.dlt_parameters) String parameters,
                       @RequestParam(defaultValue = ParamKey.In.empty) String callback) {
        ParamValue<MetaBudgetRO> paramValue = GsonUtil.json2T(parameters, new TypeToken<ParamValue<MetaBudgetRO>>() {
        }.getType());
        ParamCommon common = paramValue.getCommon();
        MetaBudgetRO ro = paramValue.getData();

        ActionResultRO obj = metaBudgetService.delete(ro.getId());
        PrintCodeUtils.print(response, obj, callback);
    }



}
